import cityData from "../../assets/citys.json";
export default {
  name: "cityCascader",
  props: {
    citys: Array,
    level: String, // 层级: '0'->全国, '1'->省, '2'->市, '3'->区
    notRequire: Boolean, // 是否不需要权限控制,清除重置成所给的权限,默认为false
	disable:{
		default: false,
		type: Boolean
	}
  },
  data() {
    return {
      value: [],
      cityLists: [],
      cityProps: {
        value: "code",
        label: "name",
        children: "subLevelModelList",
      },
      disabled: false
    }
  },
  created() {
    this.value = this.citys;
    this.handleCityLists();
	this.disabled = this.disable;
    // this.disabled = this.level === '3' ? true : false;
  },
  watch: {
    citys(newVal) {
      this.value = newVal;
    },
	disable(){
		this.disabled = this.disable;
	}
  },
  methods: {
    // 处理城市数据
    handleCityLists() {
      let cityLists = cityData.city;
      if (this.level === '1') {
        cityLists.forEach(el => {
          if (el.code === this.citys[0]) {
            this.cityLists.push(el);
            return;
          }
        });
      } else if (this.level === '2') {
        let tempObj = {};
        cityLists.forEach(el => {
          if (el.code === this.citys[0]) {
            Object.assign(tempObj, el);
          }
        });
        tempObj.subLevelModelList.forEach(el => {
          if (el.code === this.citys[1]) {
            tempObj.subLevelModelList = [];
            tempObj.subLevelModelList.push(el);
            this.cityLists.push(tempObj);
            return;
          }
        })
      } else {
        this.cityLists = cityLists;
      }
    },
    handleChange(value) {
		console.log(this.notRequire);
        if (!this.notRequire && value.length === 0) {
          let userinfo = localStorage.getItem('userinfo');
          userinfo = JSON.parse(userinfo);
          value.push(+userinfo.province);
          value.push(+userinfo.city);
          value.push(+userinfo.area);
        }
        this.$emit("handleChange", value);
    }
  }
}